package com.ffcs.cn.action.labelBase.runprocess;

import com.supermap.desktop.core.utilties.LogUtilities;
import com.supermap.desktop.core.utilties.WorkflowUtilities;
import com.supermap.iobjects.common.convertor.ConvertorUtils;
import com.supermap.sps.core.parameters.*;
import com.supermap.sps.core.parameters.impls.ConstantValueProvider;
import com.supermap.sps.core.utils.ClassUtils;

import java.lang.reflect.Array;
import java.util.List;


public class SampleCodeCollectionsValueProvider<T> implements ICollectionValueProvider<T>, IPreservableValueProvider<T> {
    private ICollectionDataDefinition<T> collectionDataDefinition;
    private IInput iInput;

    /**
     * 只能为数组和集合类型
     */
    private Object value;

    public SampleCodeCollectionsValueProvider(IInput input, Object value) {
        this.iInput = input;
        this.value = value;
        this.collectionDataDefinition = (ICollectionDataDefinition<T>) input.getDataDefinition();
    }

    @Override
    public ICollectionDataDefinition<T> getDataDefinition() {
        return this.collectionDataDefinition;
    }

    @Override
    public int size() {
        if (value == null) {
            return 0;
        } else if (value.getClass().isArray()) {
            return Array.getLength(value);
        } else if (List.class.isAssignableFrom(value.getClass())) {
            return ((List) value).size();
        }
        return 0;
    }

    @Override
    public boolean isReady() {
        if (this.iInput.isRequired()) {
            return size() > 0;
        } else {
            return true;
        }
    }

    @Override
    public void addItem(ISingleValueProvider<T> item) {

    }

    @Override
    public void addItem(T item) {

    }

    @Override
    public void removeItem(ISingleValueProvider<T> item) {

    }

    @Override
    public void removeItem(T item) {

    }

    @Override
    public int indexOf(T item) {
        return 0;
    }

    @Override
    public int indexOf(ISingleValueProvider<T> item) {
        return 0;
    }

    @Override
    public ISingleValueProvider<T> getItem(int index) {
        Object currentValue = null;
        if (value.getClass().isArray()) {
            currentValue = Array.get(value, index);
        } else if (List.class.isAssignableFrom(value.getClass())) {
            currentValue = ((List) value).get(index);
        }
        if (currentValue != null) {
            String textValue = "";
            if (WorkflowUtilities.isInlineParameterText(currentValue)) {
                textValue = currentValue.toString();
            }
            Class<T> type = this.collectionDataDefinition.getItemDefinition().getType();
            if (type.equals(currentValue.getClass()) || ClassUtils.isSameClass(type, currentValue.getClass()) || type.isAssignableFrom(currentValue.getClass())) {
                return new ConstantValueProvider<T>((ISingleDataDefinition<T>) this.collectionDataDefinition.getItemDefinition(), (T) currentValue, textValue);
            } else if (currentValue instanceof String) {
                try {
                    T convertorValue = ConvertorUtils.tryConvert(currentValue, type);
                    return new ConstantValueProvider<T>((ISingleDataDefinition<T>) this.collectionDataDefinition.getItemDefinition(), convertorValue, textValue);
                } catch (Exception e) {
                    LogUtilities.error(e);
                    return null;
                }
            }
        } else {
            return null;
        }
        // 直接抛异常出来，好定位
        return new ConstantValueProvider<T>((ISingleDataDefinition<T>) this.collectionDataDefinition.getItemDefinition(), (T) currentValue);
    }
}
